package hw2.memdir;

import hw2.FileIO;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;

public class EncList
{

    /**
     * Run this to create the encrypted directory from a plain text member list
     * The member list must contain a last name, white space, email address on
     * each line
     * 
     * @param args
     *            not used
     * @throws Exception
     * 
     */
    public static void main( String[] args ) throws Exception {

        BufferedReader in = new BufferedReader( new InputStreamReader(
                System.in ) );
        String psswrd = "";
        System.out.println( "Enter quit to exit." );
        System.out.print( "Password >> " );
        psswrd = in.readLine();
        if( psswrd.equalsIgnoreCase( "quit" ) ) {
            System.out.println( "Goodbye." );
            System.exit( 0 );
        }
        // generate the encrypted list with the provided password
        Directory dir = new Directory( new File( "data/memberList.txt" ),
                psswrd );
        FileOutputStream fos = new FileOutputStream( new File( "data/dir.enc" ) );
        ObjectOutputStream out = new ObjectOutputStream( fos );

        // store the IV used for the last name list
        FileIO.writeBytesToFile( dir.getNameIV(), "data/iv.bin" );

        // store the directory
        out.writeObject( dir );

        // close streams
        out.close();
        fos.close();

        System.out.println( "Encrypted directory and IV created." );
    }
}
